﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using System.Web.Routing;
using System.Web.Mvc.Html;
namespace THS.HtmlHelpers
{
    public static class FriendlyUrlHelper
    {
        public class Friendly
        {
            public string Url { get; set; }
            public string ActionName { get; set; }
            public string ControllerName { get; set; }
            public RouteValueDictionary RouteValues { get; set; }
            public MvcHtmlString HtmlSafeUrl
            {
                get
                {
                    return MvcHtmlString.Create(Url);
                }
            }
        }
        public static MvcHtmlString FriendlyTextUrl(this HtmlHelper helper, String strLinkText, String actionName, String controllerName, RouteValueDictionary routeValues, Dictionary<String,String> htmlAttributes=null)
        {
            var strUlr = UrlHelper.GenerateUrl("THSDetails", actionName, controllerName, routeValues, helper.RouteCollection, helper.ViewContext.RequestContext, false);
            // var url = helper.RouteLink(strLinkText, "THSDetails", routeValues,htmlAttributes);maybe for Localization
            var url = MvcHtmlString.Create(strUlr);
            //
            var link = new TagBuilder("a");
            link.MergeAttribute("href", url.ToHtmlString());
            link.MergeAttributes(htmlAttributes);
            link.InnerHtml = strLinkText;
            return MvcHtmlString.Create(link.ToString());

        }
        public static string FriendlyImageUrl(this HtmlHelper helper, string imageUrl, string actionName, String controllerName, RouteValueDictionary routeValues, Dictionary<string, string> imgAttributes=null, Dictionary<string, string> htmlAttributes=null, string Textlink = "Click here")
        {
            var imgBuilder = new TagBuilder("img");
            imgBuilder.MergeAttribute("src", imageUrl);
            imgBuilder.MergeAttributes(imgAttributes);
            var strUlr = UrlHelper.GenerateUrl("THSDetails", actionName, controllerName, routeValues, helper.RouteCollection, helper.ViewContext.RequestContext, false);
           //var url = helper.RouteLink(Textlink, "THSDetails", routeValues, htmlAttributes); maybe for Localization
            var url = MvcHtmlString.Create(strUlr);
            //
            var link = new TagBuilder("a");
            link.MergeAttribute("href", url.ToHtmlString());
            link.MergeAttributes(htmlAttributes);
            link.InnerHtml = imgBuilder.ToString();
            return link.ToString();

        }
        //public static string FriendlyImageUrl(this HtmlHelper helper, string imageUrl, string altText, string actionName, String controllerName, RouteValueDictionary routeValues,object htmlAttributes, int width=0, int height=0)
        //{
        //    var imgBuilder = new TagBuilder("img");
        //    imgBuilder.MergeAttribute("src", imageUrl);
        //    imgBuilder.MergeAttribute("alt", altText);
        //    if (width > 0 && height > 0)
        //    {
        //        imgBuilder.MergeAttribute("width", width.ToString(), true);
        //        imgBuilder.MergeAttribute("height", height.ToString(), true);
        //    }
        //    //
        //    var strUlr = UrlHelper.GenerateUrl("THSDetails", actionName, controllerName, routeValues, helper.RouteCollection, helper.ViewContext.RequestContext, false);
        //    var url = helper.RouteLink(altText, "THSDetails", routeValues,htmlAttributes);
        //    url = MvcHtmlString.Create(strUlr);
        //    //
        //    var link = new TagBuilder("a");
        //    link.MergeAttribute("href", url.ToHtmlString());
        //    link.InnerHtml = imgBuilder.ToString();
        //    return link.ToString();
            
        //}

        //public static MvcHtmlString FriendlyImageUrl(this HtmlHelper helper, String strLinkText, String actionName, String controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        //{
        //    var strUlr = UrlHelper.GenerateUrl("THSDetails", actionName, controllerName, routeValues, helper.RouteCollection, helper.ViewContext.RequestContext, false);
        //    var url = helper.RouteLink(strLinkText, "THSDetails", routeValues);
        //    url = MvcHtmlString.Create(strUlr);
        //    var link = new TagBuilder("a");
        //    link.MergeAttribute("href", url.ToHtmlString());
        //    link.InnerHtml = strLinkText;
        //    return MvcHtmlString.Create(link.ToString());

        //}
        private static String ConvertActionAndControllerName(String rawurl)
        {
            String returnPath = String.Empty;
            if (rawurl.Contains("MembersManagement/Details"))
              returnPath=  rawurl.Replace("MembersManagement/Details", "Quản-lý-thành-viên/Chi-tiết");
            else if (rawurl.Contains("AdminManagement/Details"))
                rawurl.Replace("AdminManagement/Details", "Quản-trị/Chi-tiết");
            return rawurl;
            //switch (rawurl)
            //{
            //    case "MembersManagement/Details":
            //        returnPath = "Quản-lý-thành-viên/Chi-tiết";
            //        break;
            //    case"AdminManagement/Details":
            //        returnPath = "Quản-trị/Chi-tiết";
            //        break;
            //    default:
            //        returnPath = rawurl;
            //        break;

            //}
            return returnPath;
        }

    }
}